package com.example.demo.controller;

import com.example.demo.entity.shoppingCart.ShoppingCartInfo;
import com.example.demo.service.shoppingCart.ShoppingCartService;
import com.example.demo.service.shoppingCart.dto.ShoppingCartInfoDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping("/api/v2/shoppingCar")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping
    public ResponseEntity<ShoppingCartInfo> addShoppingCart(@RequestBody ShoppingCartInfoDto shoppingCartInfoDto) {
        return ResponseEntity.ok(shoppingCartService.addtogoods(shoppingCartInfoDto));
    }

    @GetMapping("/{id:\\d+}")
    public ResponseEntity<ShoppingCartInfo> getShoppingCartById(@PathVariable Long id) {
        if (Objects.isNull(id)) {
            return null;
        }
        return ResponseEntity.ok(shoppingCartService.getshoppingCartInfoById(id));

    }

    @DeleteMapping("/{d:\\d+}")
    public ResponseEntity<Boolean> deleteShoppingCartById(@PathVariable Long id) {
        if (Objects.isNull(id)|| id<=0) {
            return null;
        }
        return ResponseEntity.ok(shoppingCartService.deleteShoppingCart(id));
    }



}
